import express from "express";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import cookieSession from "cookie-session";
import multer from "multer";

//创建express的实例对象
const app = express();

//2.配置中间件
app.use(bodyParser.urlencoded({
    extended: false,
    limit: "300mb"
}))
app.use(bodyParser.json());
app.use(cookieParser("goujiuwang"));
app.use(cookieSession({
    name: "goujiuwang",
    keys: ["aa", "bb", "cc", "dd"],
    maxAge: 1000 * 60 * 40
}))

const upload = multer({
    dest: "./../src/upload/"
});
app.use(upload.any());

app.use("*", (request, response, next) => {
    request.header("Content-Type", "application/json;charset=utf-8");
    //设置权限
    if (request.originalUrl.indexOf("/upload") != -1 ||
        request.originalUrl.indexOf("/js") != -1 ||
        request.originalUrl.indexOf("/index") != -1 ||
        request.originalUrl.indexOf("/css") != -1 ||
        request.originalUrl.indexOf("/images") != -1 ||
        request.originalUrl.indexOf("/details") != -1 ||
        request.originalUrl.indexOf("/login") != -1 ||
        request.originalUrl.indexOf("/header") != -1 ||
        request.originalUrl.indexOf("/bottom") != -1 ||
        request.originalUrl.indexOf("/reg") != -1) {
        next();
    } else {
        if (request.session["users"] == null || request.session["users"] == undefined) {
            response.json({
                msg: "请先登录",
                status: -1
            })
        } else {
            next();
        }
    }
})
//静态资源服务器
app.use(express.static("./../src"));


//使用api路由对象 (保安大爷)

app.use("/api", require("./routes/apiRoute").apiRouter)


app.listen(8082, () => {
    console.log("服务启动完毕,http://127.0.0.1:8082");
})